/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.capiq.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class SalaireBookmarkletWidget extends Composite {

	interface SalaireBookmarkletBinder extends UiBinder<Widget, SalaireBookmarkletWidget> {}

	private static SalaireBookmarkletBinder	uiBinder	= GWT.create(SalaireBookmarkletBinder.class);

	@UiField
	TextBox									maestroId;

	@UiField
	TextBox									secuNumber;

	@UiField
	Anchor									bookmarklet;

	public SalaireBookmarkletWidget() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiHandler("generate")
	public void generate(ClickEvent e) {

		String maestroIdValue = maestroId.getValue();
		String secuNumberValue = secuNumber.getValue().trim();

		StringBuffer sb = new StringBuffer();

		sb
				.append(
						"javascript:(function(){document.open();uri='https://ecoles.excilys.com/maestro-ref/personne/salairePersonne.htm?action=Valider&idPersonne=") //
				.append(maestroIdValue)//
				.append("&numeroSecu=") //
				.append(secuNumberValue) //
				.append("&numeroSecu1=") //
				.append(secuNumberValue.substring(0, 1)) //
				.append("&numeroSecu2=") //
				.append(secuNumberValue.substring(1, 3)) //
				.append("&numeroSecu3=") //
				.append(secuNumberValue.substring(3, 5)) //
				.append("&numeroSecu4=") //
				.append(secuNumberValue.substring(5, 7)) //
				.append("&numeroSecu5=") //
				.append(secuNumberValue.substring(7, 10)) //
				.append("&numeroSecu6=") //
				.append(secuNumberValue.substring(10, 13)) //
				.append("&numeroSecu7=") //
				.append(secuNumberValue.substring(13, 15)) //
				.append(
						"';f=document.createElement('form');f.method='post';f.action=uri;b=document.createElement('body');b.appendChild(f);h=document.getElementsByTagName('html')[0];h.appendChild(b);f.submit();})()");

		bookmarklet.setHref(sb.toString());
		bookmarklet.setVisible(true);

	}

}
